<?php
namespace app\api\controller;
use app\common\controller\BaseApi;

/*第二版接口 api*/
class OcmApi extends  BaseApi
{
	protected $shop_id;//店铺id
	protected $api_version;//店铺用的api版本
	protected $system_set;// 店铺的系统设置
	/**
	 * 测试地址
	 * http://SmartGYM.com/api/OcmApi/InSwipeCard/
	 * */
	public function _initialize()
    {
    	//parent::_initialize();
    }
	/**
	 * 方法前操作获取店铺信息
	 */
	function _before_method($shop_id){
		if(empty($shop_id)){
			$this->ajax_error('','',"店铺id 为空");
		}else{
			$this->shop_id=$shop_id;
		}
		$shopStatus=cache("ShopStatus_".$shop_id);
		if($shopStatus===FALSE){
			cache("ShopStatus_".$shop_id,db("Shop")->where("id=".$this->shop_id)->value("status"));
			$shopStatus=cache("ShopStatus_".$shop_id);
		}
		if(empty($shopStatus)){
			$this->ajax_error('','','您的场馆账号异常！请和管理员联系');
		}
		
		$config=cache("ShopConfig_".$this->shop_id);
    	if(empty($config)){
			 $config=db("Shop")->where("id=".$this->shop_id)->find();
			 cache("ShopConfig_".$this->shop_id,$config);
    	} 
		$this->api_version=$config['api_version'];
		
		$config1=cache("SystemSetConfig_$shop_id");
    	if(empty($config1)){
			 $config1=db("SystemSet")->where("shop_id=$shop_id")->column('code,pvalue');
			 cache("SystemSetConfig_$shop_id",$config1);
    	} 
    	$this->system_set=$config1;
	}
	/**
	 * http://smartgym.com/api/OcmApi/InSwipeCard/card_no/0723322990/cabinet_nos/男1001,男2221/shop_id/5/brakemachine_no/01
	 * 
	 * @param $card_no 卡号
	 * @param $brakemachine_no 闸机编号
	 * @param $cabinet_no 柜号
	 * @param $shoop_id 店铺id
	 */
	function InSwipeCard(){
		$this->_before_method(input("shop_id"));
		$this->_InSwipeCard(input("card_no"),input("brakemachine_no"),input("cabinet_nos"),input("shop_id"),input("has_txt"));
	}
	
	/**
	 * http://smartgym.com/api/OcmApi/OutSwipeCard/card_no/2238028450/cabinet_nos/男1001,男2221/shop_id/2/brakemachine_no/01/has_store/0
	 * 
	 * @param $card_no 卡号
	 * @param $brakemachine_no 闸机编号
	 * @param $cabinet_no 柜号
	 * @param $shoop_id 店铺id
	 * @param $has_store 是否存储了东西
	 */
	function OutSwipeCard(){
		$this->_before_method(input("shop_id"));
		$this->_OutSwipeCard(input("card_no"),input("brakemachine_no"),input("cabinet_nos"),input("shop_id"),input("has_store",0),input("face_url",""));
	}
	/**
	 * http://smartgym.com/api/OcmApi/SwipeFingerPrintResult/card_no/2238028450/shop_id/2/brakemachine_no/01/flag/true
	 * 
	 * @param $card_no 卡号
	 * @param $brakemachine_no 闸机编号
	 * @param $flag 生物验证 成功 or 失败 
	 * @param $shoop_id 店铺id
	 * @param $is_open  闸机们是否开了 .开了就不走流程了
	 */
	function SwipeFingerPrintResult(){
		$this->_before_method(input("shop_id"));
		$this->_SwipeFingerPrintResult(input("card_no"),input("brakemachine_no"),input("flag"),input("shop_id"),input("face_url",""),input("is_open",0));
	}
	/**
	 *http://smartgym.com/api/OcmApi/UploadModelData/card_no/0723109292/shop_id/2/new_datas/123
	 *
	 * @param $card_no 卡号
	 * @param $new_datas 新的数据
	 * @param $shop_id 店铺id
	 */
	function UploadModelData(){
		//$this->_UploadModelData(input("card_no"),input("new_datas"),input("shop_id"));
	}
	/**
	 * http://smartgym.com/api/OcmApi/InnerDoorSwipe/card_no/0723109292/shop_id/2/brakemachine_no/01
	 *
	 * @param $card_no
	 * @param @bramemachine_no,
	 * @param $shop_id
	 */
	function InnerDoorSwipe($card_no,$brakemachine_no,$shop_id){
		$this->_InnerDoorSwipe($card_no,$brakemachine_no,$shop_id);
	}
	
	/**
	 * http://smartgym.com/api/OcmApi/InnerDoorSwipe/card_no/0723109292/shop_id/2/brakemachine_no/01
	 *
	 * @param $card_no
	 * @param @bramemachine_no,
	 * @param $shop_id
	 */
	function InnerDoorCallBack($card_no,$brakemachine_no,$flag,$shop_id){
		$this->_InnerDoorCallBack($card_no,$brakemachine_no,$flag,$shop_id);
	}
	
	
	
	
	//////////////////////////////////////////////////////////////////////////////////////////////////////////
	 /*入场操作逻辑*/
	 function _InSwipeCard($card_no,$brakemachine_no,$cabinet_nos='',$shop_id,$has_txt){
	 	   $crash_card_no=$card_no;
	 	   $interface=action("Interfaces/get_old_interface",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		   //真正的卡号
		   $card_no=$interface['card_no'];
		   
		  if($interface['owner_type']==1){
		  	//获取合同号
		 	$member= $this->GetContractInfo($interface['card_no'], $shop_id,$interface['mobile']);
			if(!$member['flag']){
			 	action("BehaviorLog/add_error_in_log",[$interface,$brakemachine_no,$member['msg']]);
			 	$this->ajax_error("","",$member['msg'],false);
			 } 	
		  } 
		   //新增柜子记录
		   action("BehaviorLog/add_cabinet_no",['interface'=>$interface,"cabinet_nos"=>$cabinet_nos,'shop_id'=>$shop_id]);
		   //判断进出顺序
		   action("BehaviorLog/sequence_judge_out",['interface'=>$interface,"system_set"=>$this->system_set,"brakemachine_no"=>$brakemachine_no]);
		   //权限判断
		   action("BehaviorLog/auth",['interface'=>$interface,"system_set"=>$this->system_set,"brakemachine_no"=>$brakemachine_no]);
		   //卡判断
		if($interface['owner_type']==1||$interface['owner_type']==2|$interface['owner_type']==5){//会员卡或者员工卡或者副卡================================================
		   	  if($interface['in_varifycation']==1){//需要验证
		   	  	  if($interface['in_varifycation_type']==1){// 指纹验证
		   	  	      if(strlen($interface['fingerprint'])<500){
		   	  	           action("BehaviorLog/add_error_in_log",[$interface,$brakemachine_no,"指纹模板不正确"]);
		   	  	      	   $this->ajax_error("","","指纹模板不正确",false);
		   	  	      }
					  $is_down=db("ModelDownRecord")->where("card_no='$crash_card_no' and shop_id=$shop_id and type=1 and is_down=1 ")->count(1);
					  if($has_txt[0]==1&&$is_down==1){//本地有模板且已经下载
		   	  	        	$this->ajax_success("11","","",false,$interface['in_varifycation_type']."");
		   	  	      }
		   	  	  	  $this->ajax_success($interface['fingerprint'],"","",false,$interface['in_varifycation_type']."");
		   	  	  }elseif($interface['in_varifycation_type']==2){//人脸验证
		   	  	  	  $facecode=$this->_get_face_code($interface,$brakemachine_no,$has_txt,$crash_card_no);
					  $this->ajax_success($facecode,$interface['card_no'],"",false,$interface['in_varifycation_type']."");
		   	  	  }elseif($interface['in_varifycation_type']==3){//前台验证
		   	  	  	  $this->_varify_front($card_no,$shop_id,$interface,$brakemachine_no);
		   	  	  }
		   	  	  else{
		   	  	  	 $this->ajax_error("","","in_varifycation_type 错误",false);
		   	  	  }
		   	  }else{//不需要验证
		   	    if($interface['owner_type']==2){//如果是员工就放行
		   	    	action("BehaviorLog/add_success_in_log",[$interface,$brakemachine_no]);
		   	  	 	$this->ajax_success("","",$this->get_tv_tmpl($interface['shop_id'], $interface['member_id'], $interface['name'], $interface['owner_type'], $interface['card_no']),TRUE);	
		   	    }else{//如果是会员则调用第三方入场接口再次验证
		   	    	$this->_member_can_in($interface,$this->system_set,$brakemachine_no);
		   	    }
		   	  }
		   }elseif($interface['owner_type']==3){// 临时卡================================================
		   	   $this->_member_can_in($interface,$this->system_set,$brakemachine_no);
		   }else{
		   	   $this->ajax_error("owner_type 错误");
		   }
	 }
	 /*出场操作逻辑*/
	 function _OutSwipeCard($card_no,$brakemachine_no,$cabinet_nos,$shop_id,$has_store,$face_url){
	 	 $interface=action("Interfaces/get_old_interface",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		   //新增柜子记录
		 action("BehaviorLog/add_cabinet_no",['interface'=>$interface,"cabinet_nos"=>$cabinet_nos,'shop_id'=>$shop_id]);
		  // 判断是否可以出
		  $this->_member_can_out($interface,$this->system_set,$brakemachine_no,$has_store);
		  //判断进出顺序
		 action("BehaviorLog/sequence_judge_in",['interface'=>$interface,"system_set"=>$this->system_set,"brakemachine_no"=>$brakemachine_no]);
		
		//出场成功 
		$behaviorlog=db("BehaviorLog")->where("shop_id=".$interface['shop_id']." and card_no='".$interface['card_no']."'")->order("id desc")->find();
		if(!empty($behaviorlog)){
			if(empty($behaviorlog['out_time'])){
				action("BehaviorLog/add_success_out_log",[$behaviorlog,$brakemachine_no,$face_url]);
			}
		}
		 $this->ajax_success("","","",TRUE,"0");
		 
	 }
	 /*生物验证回掉逻辑*/
	 function _SwipeFingerPrintResult($card_no,$brakemachine_no,$flag,$shop_id,$face_url,$is_open){
	 	 if($is_open==1){//调用inswipe 的时候开们了，此时只是上传接口
	 	 	if(empty($face_url)){
	 	 		$this->ajax_error("","","人脸图片为空",false,"0");
	 	 	}else{
	 	 		$today=date('Y-m-d');
	 	 		$list=db("BehaviorLog")->field("id,card_no,face_url")->where("card_no='$card_no' and shop_id=$shop_id and create_time like '$today%'")->order("id desc")->limit(1)->select();
				if(empty($list)){
					$this->ajax_error("","","今天没有入场记录",false,"0");
				}else{
					if(empty($list[0]['face_url'])){
						db("BehaviorLog")->where('id='.$list[0]['id'])->update(array('face_url'=>$face_url));
						$this->ajax_success("","","",TRUE,"0");
					}else{
						$this->ajax_error("","","没有最新的入场记录",false,"0");
					}
				}
	 	 	}
	 	 }else{
	 	 	$interface=action("Interfaces/get_old_interface",['card_no'=>$card_no,'shop_id'=>$shop_id]);
			 if($flag===true||strcasecmp($flag, 'true')==0||$flag==1){
			 		$this->_member_can_in($interface,$this->system_set,$brakemachine_no,$face_url);	
			 }else{
			 	if($interface['in_varifycation_type']==1){
			 		action("BehaviorLog/add_error_in_log",[$interface,$brakemachine_no,"指纹验证失败"]);
					$this->ajax_error("","","指纹验证失败",false,"0");
			 	}elseif($interface['in_varifycation_type']==2){
			 		action("BehaviorLog/add_error_in_log",[$interface,$brakemachine_no,"人脸验证失败"]);
					$this->ajax_error("","","人脸验证失败",false,"0");
			 	}else{
			 		$this->ajax_error("","","无效的 in_varifycation_type",false,"0");
			 	}
			 }	
	 	 }
	 }
	 /*
	  * 上传模板数据逻辑
	  * */
	 function _UploadModelData($card_no,$new_datas,$shop_id){
	 	$datas=db("ModelData")->where("card_no='$card_no' and shop_id='$shop_id'")->value("datas");
		if(empty($datas)){
			db("ModelData")->insert(array("card_no"=>$card_no,'datas'=>$new_datas,'shop_id'=>$shop_id,'varify_type'=>"2"));
		}else{
			$arry=explode(",",$datas);
			if(count($arry)<5){
				array_unshift($arry,$new_datas);	
				db("ModelData")->where("card_no='$card_no' and shop_id='$shop_id'")->update(array("datas"=>implode(",", $arry)));
			}else{
				unset($arry[5]);
				array_unshift($arry,$new_datas);	
				db("ModelData")->where("card_no='$card_no' and shop_id='$shop_id'")->update(array("datas"=>implode(",", $arry)));
			}
		}
		$this->ajax_success('',"","",FALSE,"0");
	 }
	 /*内们禁逻辑*/
	 function _InnerDoorSwipe($card_no,$brakemachine_no,$shop_id){
	 	$interface=action("Interfaces/get_old_interface",['card_no'=>$card_no,'shop_id'=>$shop_id]);
	 	$this->_before_method($shop_id);
	 	if($this->api_version=='ziyouren'){//智游人内门禁
		    action("ZYRApi/InnerDoor",[$interface,$brakemachine_no]);
		}
		 
		if(empty($interface['doors_auth'])){//没有权限
	      	action("BehaviorLog/add_innerdoor_in_error",[$interface,$brakemachine_no,"没有区域门禁权限"]);
	 	  	$this->ajax_error("","","没有区域门禁权限",FALSE,"0");
		 }else{
		   $auths= explode(",", $interface['doors_auth']);
		   if(in_array(intval($brakemachine_no), $auths)){
		   		$verify_type=$this->get_slidingdoor_verifytype($shop_id,$brakemachine_no);
		   		if(empty($verify_type)){
		   			action("BehaviorLog/add_innerdoor_in_success",[$interface,$brakemachine_no]);
		   	 		$this->ajax_success("","","",TRUE,"0");	
		   		}else{
		   			if(!in_array($interface['in_varifycation_type'],[1,2])){//不需要验证
		   				action("BehaviorLog/add_innerdoor_in_success",[$interface,$brakemachine_no]);
		   	 			$this->ajax_success("","","",TRUE,"0");	
		   			}
		   			$code=$this->get_characteristic_code($shop_id, $verify_type, $interface['card_no'], $interface,$brakemachine_no);
		   			$this->ajax_error($code,"","需要生物验证",FALSE,$verify_type."");
		   		}
		   }else{
		       action("BehaviorLog/add_innerdoor_in_error",[$interface,$brakemachine_no,"没有区域门禁权限"]);
		       $this->ajax_error("","","没有区域门禁权限",FALSE,"0");
		   }
		 }	
	 }
	/**
	 * 内门禁回调用
	 * */
	function _InnerDoorCallBack($card_no,$brakemachine_no,$flag,$shop_id){
		$interface=action("Interfaces/get_old_interface",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		if($flag==1){//成功
			action("BehaviorLog/add_innerdoor_in_success",[$interface,$brakemachine_no]);	
			$this->ajax_success("","","",TRUE,"0");	
		}else{//失败
			  action("BehaviorLog/add_innerdoor_in_error",[$interface,$brakemachine_no,"生物验证失败"]);
		       $this->ajax_error("","","生物验证失败",FALSE,"0");
		}
	}
	 /*第三方 获取合同信息*/
	 function GetContractInfo($card_no,$shop_id,$mobile=""){
	 	$this->_before_method($shop_id);
		if($this->api_version=='anke'){//安科接口
			return action("api/AkV2Api/GetContractInfo",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		}elseif($this->api_version=='qingniao'){//勤鸟接口
			return action("api/QNApi/GetContractInfo",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		}elseif($this->api_version==3){// 捷径接口
			
		}elseif($this->api_version=='dongxia'){//东夏接口
			return action("api/DXApi/GetContractInfo",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		}elseif($this->api_version=='locals'){//本地判断
			return action("api/LocalsApi/GetContractInfo",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		}elseif($this->api_version=='ziyoudao'){//自由岛
		     if(empty($mobile)||strlen($mobile)!=11){
		     	return $this->ajax_error("","0003","手机号码格式错误！");
		     }
			return action("api/ZYDApi/GetContractInfo",['card_no'=>$card_no,'shop_id'=>$shop_id,"mobile"=>$mobile]);
		}elseif($this->api_version=='ziyouren'){//智游人
			return action("api/ZYRApi/GetContractInfo",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		}elseif($this->api_version=='ocm'){//ocm
			return action("api/GymApi/GetContractInfo",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		}elseif($this->api_version=='jiangougou'){//健狗狗
			return action("api/JGGApi/GetContractInfo",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		}elseif($this->api_version=='vjian'){//微键
			return action("api/VJianApi/GetContractInfo",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		}
		return $this->ajax_error("","0003","非法访问！");
	 }
	 /**第三方 入场**/
	 function _CheckIn($card_no,$shop_id,$interface,$brakemachine_no=""){
	 	if($interface['owner_type']==5){
	 		$interface1=db("interface")->where("id=".$interface['original_id'])->find();
			if(empty($interface1)){
				action("BehaviorLog/add_error_in_log",[$interface,$brakemachine_no,"副卡绑定的主卡不存在"]);
		   	  	$this->ajax_error("","","副卡绑定的主卡不存在",false);
			}else{
				$card_no=$interface1['card_no'];
				$interface=$interface1;
			}
	 	}
		
		if($this->api_version=='anke'){//安科接口 $contract_id,$member_id,$shop_id
			return action("AkV2Api/CheckIn",['contract_id'=>$interface['contract_id'],'member_id'=>$interface['member_id'],'shop_id'=>$shop_id,'card_no'=>$interface['card_no']]);
		}elseif($this->api_version=='qingniao'){//勤鸟接口$card_no,$shop_id
			return action("QNApi/CheckIn",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		}elseif($this->api_version==3){// 捷径接口
			
		}elseif($this->api_version=='dongxia'){//东夏接口$card_no,$shop_id
			return action("DXApi/CheckIn",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		}elseif($this->api_version=='locals'){//本地接口
			return action("LocalsApi/CheckIn",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		}elseif($this->api_version=='ziyoudao'){//自由岛
			return action("ZYDApi/CheckIn",["interface"=>$interface,"brakemachine_no"=>$brakemachine_no]);
		}elseif($this->api_version=='ziyouren'){//智游人
			return action("ZYRApi/CheckIn",["interface"=>$interface]);
		}elseif($this->api_version=='ocm'){//ocm
			return action("GymApi/CheckIn",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		}elseif($this->api_version=='jiangougou'){//健狗狗
			return action("api/JGGApi/CheckIn",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		}elseif($this->api_version=='vjian'){//微键
			return action("api/VJianApi/CheckIn",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		}
		return $this->ajax_error("","0003","非法访问！");
	 }
	 /**第三方 出场**/
	 function _ConfirmCheckOut($interface){
	 	if($interface['owner_type']==5){
	 		$interface1=db("interface")->where("id=".$interface['original_id'])->find();
			if(empty($interface1)){
				action("BehaviorLog/add_error_in_log",[$interface,$brakemachine_no,"副卡绑定的主卡不存在"]);
		   	  	$this->ajax_error("","","副卡绑定的主卡不存在",false);
			}else{
				$interface=$interface1;
			}
	 	}
		
	 	$card_no=$interface['card_no'];
	 	$shop_id=$interface['shop_id'];
		if($this->api_version=='anke'){//安科接口 $card_no,$shop_id
			asyncronous("Api/AkV2Api/ConfirmCheckOut",['card_no'=>$card_no,'shop_id'=>$shop_id]);
			//return action("AkV2Api/ConfirmCheckOut",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		}elseif($this->api_version=='qingniao'){//勤鸟接口 $card_no,$shop_id
			//asyncronous("Api/QNApi/ConfirmCheckOut",['card_no'=>$card_no,'shop_id'=>$shop_id]);
			action("QNApi/ConfirmCheckOut",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		}elseif($this->api_version==3){// 捷径接口
			
		}elseif($this->api_version=='dongxia'){//东夏接口 $card_no,$shop_id
			asyncronous("Api/DXApi/ConfirmCheckOut",['card_no'=>$card_no,'shop_id'=>$shop_id]);
			//return action("DXApi/ConfirmCheckOut",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		}elseif($this->api_version=='locals'){//本地接口
			asyncronous("Api/LocalsApi/ConfirmCheckOut",['card_no'=>$card_no,'shop_id'=>$shop_id]);
			//return action("LocalsApi/ConfirmCheckOut",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		}elseif($this->api_version=='ziyoudao'){//自由岛
			asyncronous("Api/ZYDApi/ConfirmCheckOut",['card_no'=>$card_no,'shop_id'=>$shop_id,"member_id"=>$interface['member_id']]);
			//return action("ZYDApi/ConfirmCheckOut",['card_no'=>$card_no,'shop_id'=>$shop_id,"member_id"=>$interface['member_id']]);
		}elseif($this->api_version=='ziyouren'){//智游人
			asyncronous("Api/ZYRApi/ConfirmCheckOut",$interface);
			//return action("ZYRApi/ConfirmCheckOut",['$interface'=>$interface]);
		}elseif($this->api_version=='ocm'){//ocm
			asyncronous("Api/GymApi/ConfirmCheckOut",['card_no'=>$card_no,'shop_id'=>$shop_id]);
			//return action("ZYRApi/ConfirmCheckOut",['$interface'=>$interface]);
		}elseif($this->api_version=='jiangougou'){//健狗狗
			asyncronous("Api/JGGApi/ConfirmCheckOut",['card_no'=>$card_no,'shop_id'=>$shop_id]);
			//return action("JGGApi/ConfirmCheckOut",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		}elseif($this->api_version=='vjian'){//微键
			asyncronous("Api/VJianApi/ConfirmCheckOut",['card_no'=>$card_no,'shop_id'=>$shop_id]);
			//return action("JGGApi/ConfirmCheckOut",['card_no'=>$card_no,'shop_id'=>$shop_id]);
		}
		else{
			return $this->ajax_error("","0003","非法访问！");	
		}
	 }
     /**获取人脸模板*/
      function _get_face_code($interface,$brakemachine_no,$has_txt,$crash_card_no){
      	    $card_no=$interface['card_no'];
			$shop_id=$interface['shop_id'];
      	    $temp=db("FaceCode")->where(" card_no='$card_no' and shop_id=$shop_id")->value("face1");
      	    if(empty($temp)){
      	    	action("BehaviorLog/add_error_in_log",[$interface,$brakemachine_no,"人脸模板不正确"]);
				$this->ajax_error("","","人脸模板不正确",false);
      	    }else{
      	    	$is_down=db("ModelDownRecord")->where("card_no='$crash_card_no' and shop_id=$shop_id and type=2 and is_down=1 ")->count(1);
      	    	if($has_txt[1]==1&&$is_down==1){//本地有模板且为最新的
      	    		return "21";
      	    	}else{
					return $temp;	
      	    	}
      	    }
      }
	 /*当指纹/人脸比对同过了验证会员是否能入场*/
	  function _member_can_in($interface,$system_set,$brakemachine_no,$face_url=''){
	  	if($interface['membership_type']==1||$interface['membership_type']==3||$interface['owner_type']==3){//次卡-储值卡-零时卡
	  	    //最后一次扣次时间
	  		$behavior_log=db("BehaviorLog")->where("shop_id=".$interface['shop_id']." and card_no='".$interface['card_no']."' and sub_time is not null ")->order("id desc")->find();
		    if(empty($behavior_log)){//第一次刷卡入场
		    	//第三方入场验证
		    }else{
		    	$time_continue=$system_set['s_s_1'];//扣卡间隔时间
		    	if(empty($time_continue)){
		    		//第三方入场验证
		    	}else{
		    		$sub_time=$behavior_log['sub_time'];
					if(empty($sub_time)){
						//第三方入场验证
					}else{
						if(time()<strtotime("+$time_continue minute",strtotime($sub_time))){//还在免扣时间内
							//本地入场验证
							action("BehaviorLog/add_success_in_log",[$interface,$brakemachine_no,null,$face_url]);
							$this->ajax_success("","",$this->get_tv_tmpl($interface['shop_id'], $interface['member_id'], $interface['name'], $interface['owner_type'], $interface['card_no']),TRUE,"0");
						}else{
							//第三方入场验证
						}
					}
		    	}
		    }
	  	}elseif(in_array($interface['membership_type'], [2,4])){//年月卡 //错峰卡
	  		//第三方入场验证
	  	}
		
		//第三方入场验证
		if($interface['owner_type']==3){//临时卡
		    if($interface['surplus_num']<1){
		    	action("BehaviorLog/add_error_in_log",[$interface,$brakemachine_no,"临时卡已经失效"]);
				$this->ajax_error("","","临时卡已经失效",false,"0");
		    }
			//扣次
			db("Interface")->update(array("id"=>$interface['id'],"surplus_num"=>0));
			action("BehaviorLog/add_success_in_log",[$interface,$brakemachine_no,date("Y-m-d H:i:s"),$face_url]);
			$this->ajax_success("","",$this->get_tv_tmpl($interface['shop_id'], $interface['member_id'], $interface['name'], $interface['owner_type'], $interface['card_no']),TRUE,"0");
		}
		//第三方验证员工卡
		if($interface['owner_type']==2){//员工卡
			action("BehaviorLog/add_success_in_log",[$interface,$brakemachine_no,null,$face_url]);
			$this->ajax_success("","",$this->get_tv_tmpl($interface['shop_id'], $interface['member_id'], $interface['name'], $interface['owner_type'], $interface['card_no']),TRUE,"0");
		}
	  	$result= $this->_CheckIn($interface['card_no'],$interface['shop_id'],$interface,$brakemachine_no);
		if($result['flag']){
			action("BehaviorLog/add_success_in_log",[$interface,$brakemachine_no,date("Y-m-d H:i:s"),$face_url]);
			$this->ajax_success("","",$this->get_tv_tmpl($interface['shop_id'], $interface['member_id'], $interface['name'], $interface['owner_type'], $interface['card_no']),TRUE,"0");
		}else{
			action("BehaviorLog/add_error_in_log",[$interface,$brakemachine_no,$result['msg']]);
			$this->ajax_error("","",$result['msg'],false,"0");
		}
	  } 

      /*是否能出场*/
	  function _member_can_out($interface,$system_set,$brakemachine_no,$has_store){
		if($system_set['s_s_5']==0&&$has_store!=0){//没有开启临时出场，但是存了东西;
			action("BehaviorLog/add_error_out_log",[$interface,$brakemachine_no,"物品未取走，请先取走"]);
			$this->ajax_error("","","物品未取走，请先取走",false,"0");
		}
		
		if($interface['owner_type']==1||$interface['owner_type']==5){//会员卡/副卡
		    $this->_ConfirmCheckOut($interface);
		}elseif($interface['owner_type']==2||$interface['owner_type']==3){//员工卡 & 临时卡
		}else{
			$this->ajax_error("","","错误的owner_type",false,"0");
		}
	  } 
	  /*判断是否前台验证*/
	  function _varify_front($card_no,$shop_id,$interface,$brakemachine_no){
	  	 $systemp_set=$this->system_set;
		  if(empty($systemp_set['s_s_2'])){
		  	 $time_continue=0;
		  }else{
		  	$time_continue=$systemp_set['s_s_2'];
		 }
	     $record=db("VarifyRecord")->where("card_no='$card_no' and shop_id=$shop_id")->order("id desc")->find();
		 if(empty($record)){
		 	action("BehaviorLog/add_error_in_log",[$interface,$brakemachine_no,"请先去前台验证"]);
		 	$this->ajax_error("","","请先去前台验证",false,"0");
		 }
		 //过了免验证使时间
		 if($record['is_in']!=0&&(time()>strtotime("+$time_continue minute",strtotime($record['in_time'])))){
		 	action("BehaviorLog/add_error_in_log",[$interface,$brakemachine_no,"请先去前台验证"]);
		 	$this->ajax_error("","","请先去前台验证",false,"0");
		 }  	
		 $this->_member_can_in($interface, $this->system_set, $brakemachine_no);
	  }
	  /**
	   * 模板下载记录
	   * */
	  function ModelDownRecord($card_no,$type,$shop_id){
	  	  $this->_model_down_record_save_update(array("shop_id"=>$shop_id,"card_no"=>$card_no,"type"=>$type));
		  $this->ajax_success("","","",TRUE,"0");
	  }
	 /**
	 * 模板是否新增或者修改---做记录
	 * */
	private function _model_down_record_save_update($data){
		$shop_id=$data['shop_id'];
		$card_no=$data['card_no'];
		$type=$data['type'];
		$model=db("ModelDownRecord")->where("card_no='$card_no' and shop_id=$shop_id and type=$type ")->field("id,card_no")->find();
		if(empty($model)){
			db("ModelDownRecord")->insert(array("card_no"=>$card_no,"shop_id"=>$shop_id,"type"=>$type,"is_down"=>1));
		}else{
			$model['is_down']=1;
			db("ModelDownRecord")->update($model);
		}
	}
	
	/**
	 * 获取tv 盒子模板信息
	 * @param $shop_id
	 * @param $member_id
	 * @param $member_name
	 * */
	function get_tv_tmpl($shop_id,$member_id,$member_name,$owner_type,$card_no){
		$count=db("Equipment")->where("shop_id=$shop_id and type=3")->count(1);//没有tv盒子
		if(empty($count)){
			return "no tv";
		}
		$tpl=db("TvboxConfig")->where("shop_id=$shop_id")->value("welcome_tpl");
		if(in_array($owner_type,[1,5])){
			$owner_type="会员";
			$in_count=db("BehaviorLog")->where("member_id='$member_id' and shop_id=$shop_id")->count(1);
		}else if($owner_type==2){
			$in_count=db("BehaviorLog")->where("shop_id=$shop_id and card_no='$card_no'")->count(1);
			$owner_type="员工";
		}else if($owner_type==3){
			$in_count=1;
			$owner_type="临时会员";
		}else{
			$owner_type="";
		}
		if(empty($tpl)){
			$tpl="'$member_name', '$owner_type' ,欢迎您第 $in_count 次入场锻炼，要坚持打卡哦！";
		}else{
			$tpl=str_replace(["{{member_name}}","{{in_count}}"], [$member_name,$in_count], $tpl);
		}
		return $tpl;
	}
	/**
	 * 判读内门禁是否有权限
	 * @param shop_id 店铺id
	 * */
	function get_slidingdoor_verifytype($shop_id,$brakemachine_no){
		cache("SlidingDoor_".$shop_id,null);
		$config=cache("SlidingDoor_".$shop_id);
		if(empty($config)){
			$config=db("SlidingDoor")->where("shop_id=$shop_id")->column("code,verify_type");
			cache("SlidingDoor_".$shop_id,$config);	
		}
		$brakemachine_no=intval($brakemachine_no);
		return $config[$brakemachine_no];
	}
	/**
	 * 内门禁获取生物特征码
	 * */
	function get_characteristic_code($shop_id,$verify_type,$card_no,$interface,$brakemachine_no){
		 if($verify_type==1){
		 	$finger_print=db("Interface")->where("card_no='$card_no' and shop_id=$shop_id")->value("fingerprint");
			 if(empty($finger_print)||strlen($finger_print)<1000){
				action("BehaviorLog/add_innerdoor_in_error",[$interface,$brakemachine_no,"没有录入人脸模板"]);
				$this->ajax_error("","","没有录入指纹模板",FALSE,"0"); 	
			 }else{
			 	return $finger_print;
			 }
		 }else if($verify_type==2){
		 	$face1=db("FaceCode")->where("card_no='$card_no' and shop_id=$shop_id")->value("face1");
			 if(empty($face1)||strlen($face1)<2000){
				action("BehaviorLog/add_innerdoor_in_error",[$interface,$brakemachine_no,"没有录入人脸模板"]);
				$this->ajax_error("","","没有录入人脸模板",FALSE,"0"); 	
			 }else{
			 	return $face1;
			 }
		 }else{
		 	action("BehaviorLog/add_innerdoor_in_error",[$interface,$brakemachine_no,"错误的verify_type"]);
			$this->ajax_error("","","错误的verify_type",FALSE,"0");
		 }
	} 
	///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
